<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Cache extends Gio_Core_Controller
{
	public function indexAction()
	{
		$request = $this->getRequest();
		$types = array();
		$cacheDir = TEMP_DIR . DS . 'cache';
		if (!file_exists($cacheDir)) {
			return;
		}
		
		if ($request->isPost()) {
			$this->setNoRender();
			$this->disableLayout();
			
			$type = $request->getPost('cache_type');
			$numFiles = Gio_Core_Cache::clean($type);
			$this->getResponse()->setBody('RESULT_OK');			
			return;
		}
		
		$cacheIterator = new DirectoryIterator($cacheDir);
		foreach ($cacheIterator as $dir) {
			if ($dir->isDot()) {
				continue;
			}
			$dirName = $dir->getFilename();
			if (('CVS' == $dirName) || ('.svn' == strtolower($dirName)) 
					|| ('index.html' == $dirName)
					|| ('.htaccess' == $dirName)) {
                continue;
            }
            $type = array();
            $type['name'] = $dirName;
            
            $dirType = $cacheDir . DS . $dirName;
			$cacheFiles = @glob($dirType . DS . Gio_Core_Cache::CACHE_FILE_PREFIX . '*');
			$type['num_files'] = count($cacheFiles);
			
			$types[] = $type;
		}
		$this->view->types = $types;
	}
}